package servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String uploadPath = "NMS\\WebRoot\\img";

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        
        InputStream inputStream = request.getInputStream();
        String imageName = request.getParameter("imageName");
        FileOutputStream outputStream = new FileOutputStream(uploadPath + "\\" + imageName);

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        doPost(request, response);
    }
}